package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author suchengchen
 * @date 2023/8/21
 * @description
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("admin/acl/user")
//@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    @ApiOperation("获取用户角色")
    @GetMapping("toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId) {
        Map<String,Object> map  = roleService.getRoleByAdminId(adminId);
        return Result.ok(map);
    }

    @ApiOperation("为用户分配角色")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long adminId, @RequestParam Long[] roleId) {
        roleService.saveAdminRole(adminId, roleId);
        return Result.ok(null);
    }

    @ApiOperation("用户列表")
    @GetMapping("{current}/{limit}")
    public Result list(@PathVariable Long current, @PathVariable Long limit, AdminQueryVo adminQueryVo) {
        Page<Admin> adminPage = new Page<Admin>(current, limit);
        Page<Admin> pageModel = adminService.selectPageUser(adminPage, adminQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("id查询用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable String id) {
        Admin byId = adminService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation("添加用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin) {
        admin.setPassword(MD5.encrypt(admin.getPassword()));
        boolean save = adminService.save(admin);
        return Result.ok(null);
    }

    @ApiOperation("修改")
    @PutMapping("update")
    public Result update(@RequestBody Admin admin) {
        boolean b = adminService.updateById(admin);
        return Result.ok(null);
    }

    @ApiOperation("id删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean b = adminService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result remove(@RequestBody List<Long> ids) {
        boolean b = adminService.removeByIds(ids);
        return Result.ok(null);
    }
}
